layui.use(['table', 'LayerForm', 'LayerAjax'], function () {
    var table = layui.table;
    var exbase = layui.exbase;
    var $ = layui.$;

    var userTable = table.render({
        elem: '#user_table_id',
        height: 'full-60',
        id: 'user_table_id',
        url: ctxPath + '/user/page',
        method: 'POST',
        contentType: 'application/json',
        dataType: 'json',
        page: {
            layout: ['limit', 'count', 'prev', 'page', 'next', 'skip'],
            first: false, //不显示首页
            last: false //不显示尾页
        },
        cols: [[ //表头
            {checkbox: true}
            , {field: 'id', title: 'ID', width: 80, sort: true, hide: true}
            , {field: 'accountCode', title: '账号'}
            , {field: 'name', title: '用户名'}
            , {field: 'createTime', title: '创建时间'}
            , {field: 'updateTime', title: '修改时间'}
        ]],
        parseData: function (res) {
            return {
                "code": res.errorCode,
                "msg": res.errorMsg,
                "count": res.data.total,
                "data": res.data.records
            }
        },
    });

    $("#add_button_id").on('click', function () {
        var form = new layui.LayerForm({
            url: ctxPath + "/user/create",
            method: 'POST',
            title: '创建用户',
            contentType: 'application/json',
            success: function () {
                userTable.reload();
            },
            form: [{
                name: "accountCode",
                label: "账号",
                type: 'text',
                verify: 'required'
            }, {
                name: "name",
                label: "姓名",
                type: 'text',
                verify: 'required'
            }]
        })
        form.open()
    });

    $("#delete_button_id").on('click', function () {
        var checkStatus = table.checkStatus('user_table_id');
        if (checkStatus.data.length == 0) {
            layer.alert("请选中一条数据后操作", {title: '提示'})
            return
        }
        var idList = checkStatus.data.map(a => a.id).join(",")

        var ajax = new layui.LayerAjax({
            title: "删除选中用户",
            url: ctxPath + "/user/delete",
            method: "POST",
            contentType: 'application/json',
            data: {"ids": idList},
            success: function () {
                layer.msg("操作成功")
                userTable.reload();
            }
        })
        ajax.open();
    });

    $("#reset_pwd_button_id").on('click', function () {
        var checkStatus = table.checkStatus('user_table_id');
        if (checkStatus.data.length == 0) {
            layer.alert("请选中一条数据后操作", {title: '提示'})
            return
        }
        var id = checkStatus.data[0].id

        var ajax = new layui.LayerAjax({
            title: "重置用户密码",
            url: ctxPath + "/user/resetPassword",
            contentType: 'application/json',
            data: {"id":id},
            method: "POST",
            success: function () {
                layer.msg("操作成功")
            }
        })
        ajax.open();
    });
})